home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / XDEMOS / GLXDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-21  |  2.5 KB  |  134 lines

  1. /* $Id: glxdemo.c,v 3.0 1998/02/21 02:16:54 brianp Exp $ */
  2.  
  3.  
  4. /*
  5.  * A demonstration of using the GLX functions.  This program is in the
  6.  * public domain.
  7.  *
  8.  * Brian Paul
  9.  */
  10.  
  11.  
  12. /*
  13.  * $Log: glxdemo.c,v $
  14.  * Revision 3.0  1998/02/21 02:16:54  brianp
  15.  * initial rev
  16.  *
  17.  */
  18.  
  19.  
  20. #include <GL/gl.h>
  21. #include <GL/glx.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25.  
  26.  
  27. static void redraw( Display *dpy, Window w )
  28. {
  29.    printf("Redraw event\n");
  30.  
  31.    glClear( GL_COLOR_BUFFER_BIT );
  32.  
  33.    glColor3f( 1.0, 1.0, 0.0 );
  34.    glRectf( -0.8, -0.8, 0.8, 0.8 );
  35.  
  36.    glXSwapBuffers( dpy, w );
  37. }
  38.  
  39.  
  40.  
  41. static void resize( unsigned int width, unsigned int height )
  42. {
  43.    printf("Resize event\n");
  44.    glViewport( 0, 0, width, height );
  45.    glMatrixMode( GL_PROJECTION );
  46.    glLoadIdentity();
  47.    glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  48. }
  49.  
  50.  
  51.  
  52. static Window make_rgb_db_window( Display *dpy,
  53.                   unsigned int width, unsigned int height )
  54. {
  55.    int attrib[] = { GLX_RGBA,
  56.             GLX_RED_SIZE, 1,
  57.             GLX_GREEN_SIZE, 1,
  58.             GLX_BLUE_SIZE, 1,
  59.             GLX_DOUBLEBUFFER,
  60.             None };
  61.    int scrnum;
  62.    XSetWindowAttributes attr;
  63.    unsigned long mask;
  64.    Window root;
  65.    Window win;
  66.    GLXContext ctx;
  67.    XVisualInfo *visinfo;
  68.  
  69.    scrnum = DefaultScreen( dpy );
  70.    root = RootWindow( dpy, scrnum );
  71.  
  72.    visinfo = glXChooseVisual( dpy, scrnum, attrib );
  73.    if (!visinfo) {
  74.       printf("Error: couldn't get an RGB, Double-buffered visual\n");
  75.       exit(1);
  76.    }
  77.  
  78.    /* window attributes */
  79.    attr.background_pixel = 0;
  80.    attr.border_pixel = 0;
  81.    attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  82.    attr.event_mask = StructureNotifyMask | ExposureMask;
  83.    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  84.  
  85.    win = XCreateWindow( dpy, root, 0, 0, width, height,
  86.                 0, visinfo->depth, InputOutput,
  87.                 visinfo->visual, mask, &attr );
  88.  
  89.    ctx = glXCreateContext( dpy, visinfo, NULL, True );
  90.  
  91.    glXMakeCurrent( dpy, win, ctx );
  92.  
  93.    return win;
  94. }
  95.  
  96.  
  97. static void event_loop( Display *dpy )
  98. {
  99.    XEvent event;
  100.  
  101.    while (1) {
  102.       XNextEvent( dpy, &event );
  103.  
  104.       switch (event.type) {
  105.      case Expose:
  106.         redraw( dpy, event.xany.window );
  107.         break;
  108.      case ConfigureNotify:
  109.         resize( event.xconfigure.width, event.xconfigure.height );
  110.         break;
  111.       }
  112.    }
  113. }
  114.  
  115.  
  116.  
  117. int main( int argc, char *argv[] )
  118. {
  119.    Display *dpy;
  120.    Window win;
  121.  
  122.    dpy = XOpenDisplay(NULL);
  123.  
  124.    win = make_rgb_db_window( dpy, 300, 300 );
  125.  
  126.    glShadeModel( GL_FLAT );
  127.    glClearColor( 0.5, 0.5, 0.5, 1.0 );
  128.  
  129.    XMapWindow( dpy, win );
  130.  
  131.    event_loop( dpy );
  132.    return 0;
  133. }
  134.